package com.hd.platform.util.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 17/5/9.
 */
public class MyImagePHash {
    public static final int FWIDTH = 8;
    public static final int FHEIGHT = 8;
    public static String getFingerprint(String srcPath) {
        BufferedImage img = ImageDigital.readImg(srcPath);
        int w = img.getWidth();
        int h = img.getHeight();
        int pix[] = new int[w * h];
        pix = img.getRGB(0, 0, w, h, pix, 0, w);
        long l = getFingerprint(pix, w, h);
        StringBuilder sb = new StringBuilder(Long.toHexString(l));
        if(sb.length() < 16) {
            int n = 16-sb.length();
            for(int i=0; i<n; i++) {
                sb.insert(0, "0");
            }
        }
        //System.out.println();
        return sb.toString();
    }

    public static long getFingerprint(int[] pix, int w, int h) {
        pix = AmplificatingShrinking.shrink(pix, w, h, FWIDTH, FHEIGHT);
        int[] newpix = ImageDigital.grayImage(pix, FWIDTH, FHEIGHT);
        int avrPix = averageGray(newpix, FWIDTH, FHEIGHT);
        //int hist[] = new int[FWIDTH*FHEIGHT];
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<FWIDTH*FHEIGHT; i++) {
            if(newpix[i] >= avrPix) {
                sb.append("1");
            } else {
                sb.append("0");
            }
        }
        long result = 0;
        if(sb.charAt(0) == '0') {
            result = Long.parseLong(sb.toString(), 2);
        } else {
            result = 0x8000000000000000l ^ Long.parseLong(sb.substring(1), 2);
        }

        return result;
    }
    private static int averageGray(int[] pix, int w, int h) {
        int sum = 0;
        for(int i=0; i<h; i++) {
            for(int j=0; j<w; j++) {
                sum = sum+pix[i*w + j];
            }

        }
        return (int)(sum/(w*h));
    }
    /**
     * ¼ÆËãººÃ÷¾àÀë
     * @param s1 Ö¸ÎÆÊý1
     * @param s2 Ö¸ÎÆÊý2
     * @return ººÃ÷¾àÀë
     */
    public static int hammingDistance(String s1, String s2) {
        int count = 0;
        for(int i=0; i<s1.length(); i++) {
            if(s1.charAt(i) != s2.charAt(i)) {
                count ++;
            }
        }
        return count;
    }

    public static String getFingerprintPhash(String srcPath) {
        BufferedImage img = ImageDigital.readImg(srcPath);
        if(img==null) return "";
        int w = img.getWidth();
        int h = img.getHeight();
        int pix[] = new int[w * h];
        pix = img.getRGB(0, 0, w, h, pix, 0, w);

        pix = AmplificatingShrinking.shrink(pix, w, h, 32, 32);
        pix = ImageDigital.grayImage(pix, 32, 32);
        Transformation tf = new Transformation();
        int[] dctPix = tf.DCT(pix, 32);
        int avrPix = averageGray(dctPix, FWIDTH, FHEIGHT);
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<FHEIGHT; i++) {
            for(int j=0; j<FWIDTH; j++) {
                if(dctPix[i*FWIDTH + j] >= avrPix) {
                    sb.append("1");
                } else {
                    sb.append("0");
                }
            }
        }
        long result = 0;
        if(sb.charAt(0) == '0') {
            result = Long.parseLong(sb.toString(), 2);
        } else {
            result = 0x8000000000000000l ^ Long.parseLong(sb.substring(1), 2);
        }

        sb = new StringBuilder(Long.toHexString(result));
        if(sb.length() < 16) {
            int n = 16-sb.length();
            for(int i=0; i<n; i++) {
                sb.insert(0, "0");
            }
        }
        return sb.toString();
    }

    public static  void searchFile(String filepath) throws Exception{
        File folder = new File(filepath);
        File[] subFolders = folder.listFiles();
        List<File> result = new ArrayList<File>();// 声明一个集合
        for (int i = 0;
             i < subFolders.length; i++)
        {// 循环显示文件夹或文件
            if (subFolders[i].isFile()) {
                //构建图片索引

                String phash=getFingerprintPhash(subFolders[i].getAbsolutePath());
               int distinct=0;
                if(i!=subFolders.length-1) {
                    String next = getFingerprintPhash(subFolders[i + 1].getAbsolutePath());
                    distinct = hammingDistance(phash,next);
                   /* if(distinct<=3)
                    {
                        System.out.println("src="+subFolders[i].getAbsolutePath()+",dest="+subFolders[i+1].getAbsolutePath());
                    }*/
                }
                System.out.println("source: " + phash+",distinct="+distinct);
                //文件phash写入磁盘文件
            } else {
                searchFile(subFolders[i].getAbsolutePath());

            }
        }


    }
    public static void main(String[] args) {
        String s1 = null, s2 = null;
        //s1 =  getFingerprintPhash("/work/scrapy/data/samebigfruit/11/f3a99879c218f75147b81fdd531ebcd278ad14a8.jpg");
       // s2 =  getFingerprintPhash("/work/scrapy/data/samebigfruit/12/e107535302c39023f373f01eface54bd1ddba011.jpg");
        //System.out.print(  + hammingDistance(s1, s2) + "\t");
        try {
            searchFile("/work/scrapy/data/samebigfruit/1");
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
}
